package com.android.juguotang.fragment.member;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.android.juguotang.R;
import com.android.juguotang.fragment.BaseLazyFragment;
import com.android.juguotang.model.ajaxcartCallback;
import com.android.juguotang.model.ajaxcartdata;
import com.android.juguotang.model.member.MemberInfoData;
import com.android.juguotang.model.member.MemberInfoDataCallback;
import com.android.juguotang.utils.ConstantsUrl;
import com.android.juguotang.utils.HttpUtils;
import com.android.juguotang.utils.LoginState;
import com.android.juguotang.utils.MyLog;
import com.android.juguotang.utils.ObjectUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

public class Fragment_Mine_Accounts_Card extends BaseLazyFragment {
    @BindView(R.id.tv_account_uid)
    TextView tv_account_uid;
    @BindView(R.id.et_accounts_card_number)
    EditText et_accounts_card_number;
    @BindView(R.id.et_accounts_card_pwd)
    EditText et_accounts_card_pwd;
    @BindView(R.id.et_accounts_card_scode)
    EditText et_accounts_card_scode;
    @BindView(R.id.tv_accounts_card_refill)
    TextView tv_accounts_card_refill;
    @BindView(R.id.webview_card)
    WebView webview_card;


    private FragmentActivity mcontext;
    private View view;
    private boolean networkConnected;

    public Fragment_Mine_Accounts_Card() {

    }
    public static Fragment_Mine_Accounts_Card newInstance(String price) {
        Fragment_Mine_Accounts_Card fragment = new Fragment_Mine_Accounts_Card();
        Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, price);
        fragment.setArguments(args);
        return fragment;
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private boolean isCheck = true;

    @Override
    protected void initPrepare() {

    }

    @Override
    protected void onInvisible() {

    }
    private String mobile;
    @Override
    protected void initData() {
        if (LoginState.getInstance().isLogin(mcontext)) {


        }else{

        }
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine_accounts_card, container, false);
        ButterKnife.bind(this, view);
        mcontext = getActivity();
        String path="http://www.tbfresh.cn/welcome/scode";

        String cookie = CookieManager.getInstance().getCookie(path);
        final String PHPSESSID= cookie.substring(cookie.indexOf("PHPSESSID=")).replace("PHPSESSID=","");;
        MyLog.e("inest","cookie="+cookie+"        PHPSESSID="+PHPSESSID);
        webview_card.loadUrl(path);
        tv_accounts_card_refill.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               String card_number= et_accounts_card_number.getText().toString();
                String card_pwd= et_accounts_card_pwd.getText().toString();
                String card_scode= et_accounts_card_scode.getText().toString();
                toRefill(card_number,card_pwd,card_scode,PHPSESSID);
            }


        });

        return view;
    }
    private void toRefill(String card_number, String card_pwd, String card_scode, String phpsessid) {
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBERRECCHAGECARD;
        HashMap<String, String> map = new HashMap<>();
//        MyLog.e("inest","address="+address);
        map.put("number", card_number);
        map.put("password", card_pwd);
        map.put("scode", card_scode);
        map.put("token", "adfb1aabfbaba7d5c7d3808bf134971f");
        map.put("Submit", "充值");
        HttpUtils.get(address, null, new ajaxcartCallback() {

            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(ajaxcartdata response, int id) {

            }
        });
    }
    private void GetDataNumber() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.MEMBERORDINGDANGNUM;
            MyLog.e("地址", address);
            String getid = LoginState.getInstance().getid(mcontext);
            String getpd = LoginState.getInstance().getpd(mcontext);
            HashMap<String, String> header = new HashMap<>();
            header.put("UID", getid);
            header.put("UPSW", getpd);
            HttpUtils.get(address, header, null, new MemberInfoDataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
//                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(MemberInfoData response, int id) {
                    if (response == null) {
//                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
                        return;
                    }
//                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    MemberInfoData.DataBean data = response.getData();
                    if (response.getCode() == 0 && data != null) {
                        MemberInfoData.DataBean.MemberBean member = data.getMember();
                        if (member != null) {
//                            nickname = member.getNickname();
//                            photo = member.getPhoto();
                            mobile = member.getMobile();
                            tv_account_uid.setText(mobile);
//                            UpdataInfo(nickname, photo, member.getMid());
//                            userName.setText(nickname);
//                            ObjectUtils.photoCircle(mcontext, photo, userAvatar);
                        }

                    } else {
//                        noData.setVisibility(View.VISIBLE);
//                        noDataTv.setText(response.getMsg());
//                        noDataTv.setVisibility(View.VISIBLE);
                    }
                }
            });
        } else {
//            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }
    }
}
